﻿using Entitas;
using Script.Other;

/// <summary>
/// follow System
/// </summary>
public class FollowTargetSystem : IExecuteSystem
{
    private Contexts           myContexts;
    private IGroup<GameEntity> myGroup;

    public FollowTargetSystem (Contexts contexts)
    {
        myContexts = contexts;
        myGroup    = contexts.game.GetGroup (GameMatcher.AllOf (GameMatcher.TargetComp, GameMatcher.VelComp, GameMatcher.RotComp));
    }

    public void Execute ()
    {
        foreach (var entity in myGroup.GetEntities ())
        {
            var targetEntity = myContexts.game.GetEntityWithId (entity.targetComp.targetId);

            var targetPos = targetEntity.posComp.pos;
            var selfPos   = entity.posComp.pos;
            var director  = (targetPos - selfPos).normalized;
            entity.ReplaceRotComp (director.Vector2Float ());
            entity.ReplaceVelComp (director * 1);
        }
    }
}